<?php
class Adm_right_model extends CI_Model {


	public function __construct(){
		parent::__construct();
		$this->db = $this->load->database('default',true);
	}

	/**
	 *
	 *
		* @name query_user_menu
		* @date 2013-1-9
		* @author eifel
		* @param user_id
		* @return 权限信息
		* @version 1.0
		* @description 根据用户ID查询用户角色的权限
	 */
	public function query_user_menu($user_id) {
		$user_id	=$this->db->escape($user_id);
		$sql		="select adm_right.* from adm_user_role
						inner join adm_role on adm_role.role_id =adm_user_role.role_id
						inner join adm_role_right on adm_user_role.role_id =adm_role_right.role_id
						inner join adm_right on adm_role_right.right_id =adm_right.right_id
						where adm_user_role.user_id=$user_id
						and (adm_right.parent_id=0 or adm_right.url is not null)";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}

	/**
	 *
	 *
		* @name query_admin_menu
		* @date 2013-1-9
		* @author eifel
		* @param user_id
		* @return 权限信息
		* @version 1.0
		* @description 根据用户ID查询用户角色的权限
	 */
	public function query_admin_menu() {
		$sql		="select adm_right.* from adm_right
						where adm_right.parent_id=0 or url is not null";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}
	public function query_right_list() {
		$sql ="select right_id,name,parent_id from adm_right order by right_id asc";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}
	public function get_module_right() {
		$sql	="select right_id,name from adm_right where parent_id='0' order by right_id asc";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}
	
	public function check_right($action,$user_id){
		$sql ="select adm_right.right_id 
				from adm_user 
				inner join adm_user_role on adm_user.user_id = adm_user_role.user_id
				inner join adm_role on adm_user_role.role_id =adm_role.role_id
				inner join adm_role_right on adm_role.role_id =adm_role_right.role_id
				inner join adm_right  on adm_role_right.right_id =adm_right.right_id
				where adm_user.user_id ='$user_id'
				and adm_right.action ='$action' limit 1";
		$query =$this->db->query($sql);
		if ($query->num_rows()) {
			return true;
		}
		
	}
}